/**
 * cn.ucox.web.application.config.DatabaseConfig
 *
 * @author chenw
 * @create 16/2/3.15:12
 * @email javacspring@hotmail.com
 */

package cn.com.oceansoft.application.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * 数据库配置
 *
 * @author chenw
 * @create 16/2/3 15:12
 * @email javacspring@gmail.com
 */
//@Configuration
//@Import(AppConfig.class)
//@PropertySource("classpath:properties/jdbc.properties")
public class DataSourceConfig {

    @Autowired
    AppConfig appConfig;

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final static Marker MARKER_SPRING = MarkerFactory.getMarker("spring");


    @Value("${jdbc.ds.driver}")
    private String jdbcDriverName;
    @Value("${jdbc.ds.url}")
    private String jdbcUrl;
    @Value("${jdbc.ds.username}")
    private String jdbcUserName;
    @Value("${jdbc.ds.password}")
    private String jdbcPassword;

    @Bean
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(jdbcDriverName);
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(jdbcUserName);
        dataSource.setPassword(jdbcPassword);

        logger.info(MARKER_SPRING,
                "初始化DriverManagerDataSource成功!: Driver={} Url={} User={}",
                jdbcDriverName,
                jdbcUrl,
                jdbcUserName,
                jdbcPassword);

//        dataSource.setDriverClassName(appConfig.getJdbcDriverName());
//        dataSource.setUrl(appConfig.getJdbcUrl());
//        dataSource.setUsername(appConfig.getJdbcUserName());
//        dataSource.setPassword(appConfig.getJdbcPassword());

//        logger.info(MARKER_SPRING,
//                "初始化DriverManagerDataSource成功!: Driver={} Url={} User={}",
//                appConfig.getJdbcDriverName(),
//                appConfig.getJdbcUrl(),
//                appConfig.getJdbcUserName(),
//                appConfig.getJdbcPassword());
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource(), true);
    }
}
